﻿using System;
using System.Web;
using System.Xml;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QXBook.Model;
using QXBook.Service;
using QXBook.Utils;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Data.SqlClient;

namespace QXBook.Service
{
    public class CategoriesService: ServiceBase
    {
        public int Add(CategoryInfo category)
        {
            DbCommand cmd = new SqlCommand();
            cmd.CommandText = "INSERT INTO Category(CategoryName,ParentID,ParentIDList,RelatedType) VALUES (@CategoryName,@ParentID,@ParentIDList,@RelatedType)";

            this.db.AddInParameter(cmd, "@CategoryName", DbType.String, category.CategoryName);
            this.db.AddInParameter(cmd, "@ParentID", DbType.Int32, category.ParentID);
            this.db.AddInParameter(cmd, "@ParentIDList", DbType.Int32, category.ParentIDList);
            this.db.AddInParameter(cmd, "@RelatedType", DbType.Int16, (int)category.PartType);
            this.db.ExecuteNonQuery(cmd);
            return 0;
        }

        public int Update(CategoryInfo category)
        {
            DbCommand cmd = new SqlCommand();
            cmd.CommandText = "UPDATE Category SET CategoryName=@CategoryName,ParentID=@ParentID,ParentIDList=@ParentIDList,RelatedType=@RelatedType WHERE CategoryID=@CategoryID";

            this.db.AddInParameter(cmd, "@CategoryName", DbType.String, category.CategoryName);
            this.db.AddInParameter(cmd, "@ParentID", DbType.Int32, category.ParentID);
            this.db.AddInParameter(cmd, "@ParentIDList", DbType.Int32, category.ParentIDList);
            this.db.AddInParameter(cmd, "@RelatedType", DbType.Int16, (int)category.PartType);
            this.db.AddInParameter(cmd, "@CategoryID", DbType.Int32, category.CategoryID);
            this.db.ExecuteNonQuery(cmd);
            return 0;
        }

        internal DataTable GetAllCategoryData(Part partType)
        {
            string sql = "SELECT CategoryID,CategoryName,ParentID,ParentIDList,RelatedType FROM Category WHERE 1=1 ";

            if (partType != Part.Unknow)
            {
                sql += " AND RelatedType&" + (int)partType + "=RelatedType";
            }

            DataSet ds = this.db.ExecuteDataSet(CommandType.Text, sql);

            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }

            return null;
        }

        public List<CategoryInfo> GetAllCategory(Part partType)
        {
            if (_dataSource == null)
            {
                _dataSource = GetAllCategoryData(partType);
            }
            return new List<CategoryInfo>();
        }

        private DataView CreateCategoryDataSourcde(Part partType)
        {
            if (_dataSource == null)
            {
                _dataSource = GetAllCategoryData(partType);
            }
            DataView dvTree = new DataView(_dataSource);
            return dvTree;
        }

        /// <summary>
        /// 创建无限下级分类
        /// </summary>
        /// <param name="partType"></param>
        /// <param name="parentID"></param>
        public void CreateCategory(Part partType, int parentID)
        {
            DataView dvTree = CreateCategoryDataSourcde(partType);
            dvTree.RowFilter = " ParentID=" + parentID;

            
            foreach (DataRowView Row in dvTree)
            {
                if (parentID == 0)
                {   //添加根节点
                    _stage = string.Empty;
                    CategoryInfo cate = new CategoryInfo();
                    cate.CategoryID = CommonUtils.ToInt(Row["CategoryID"]);
                    cate.CategoryName = CommonUtils.ToString(Row["CategoryName"]);
                    cate.ParentID = CommonUtils.ToInt(Row["ParentID"]);
                    cate.PartType = (Part)CommonUtils.ToInt(Row["RelatedType"]);
                    _listcategory = _listcategory ?? new List<CategoryInfo>();
                    _listcategory.Add(cate);
                    //再次递归   
                    CreateCategory(partType, cate.CategoryID);
                }
                else
                {
                    _stage += "--";
                    CategoryInfo cate = new CategoryInfo();
                    cate.CategoryID = CommonUtils.ToInt(Row["CategoryID"]);
                    cate.Stage = _stage;
                    cate.CategoryName = CommonUtils.ToString(Row["CategoryName"]);
                    cate.ParentID = CommonUtils.ToInt(Row["ParentID"]);
                    cate.PartType = (Part)CommonUtils.ToInt(Row["RelatedType"]);
                    _listcategory = _listcategory ?? new List<CategoryInfo>();
                    _listcategory.Add(cate);

                    //再次递归   
                    CreateCategory(partType, cate.CategoryID);
                }
            }
        }

        private string _stage = string.Empty;
        private DataTable _dataSource = null;
        private List<CategoryInfo> _listcategory = null;

        public List<CategoryInfo> ListCategory
        {
            get { return _listcategory ?? new List<CategoryInfo>(); }
        }

        /// <summary>
        /// 绑定分类的控件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="control"></param>
        /// <param name="partType"></param>
        /// <param name="parentID"></param>
        public void BindCategory<T>(T control, Part partType, int parentID)
        {
            this.CreateCategory(partType, parentID);
            if (_listcategory != null)
            {
                if (control is HtmlSelect)
                {
                    (control as HtmlSelect).DataSource = _listcategory;
                    (control as HtmlSelect).DataTextField = "DisplayName";
                    (control as HtmlSelect).DataValueField = "CategoryID";
                    (control as HtmlSelect).DataBind();
                }

                if (control is ListControl)
                {
                    (control as ListControl).DataSource = _listcategory;
                    (control as ListControl).DataTextField = "DisplayName";
                    (control as ListControl).DataValueField = "CategoryID";
                    (control as ListControl).DataBind();
                }
            }
        }
    }
}
